//
//  ProDailyFormCell.swift
//  ZYA
//
//  Created by 邱仙凯 on 2022/7/11.
//  Copyright © 2025 aowang. All rights reserved.
//

import UIKit

class ConfigMenuFormCell: UITableViewCell,operatable  {
    
    
    
    lazy  var itemBg:FormView = {
        let v = FormView()
        v.backgroundColor = .clear
//        v.layer.cornerRadius = 8
//        v.layer.masksToBounds = true
        return v
    }()
    lazy  var bottomBg:UIView = {
        let v = UIView()
        v.backgroundColor = .white
//        v.layer.cornerRadius = 8
//        v.layer.masksToBounds = true
        return v
    }()
 
    
    
    
    var templateDetail = [TemplateModel]()
    var itemArr = [FormItem]()
    var u_superVC:UIViewController?
    var auditParamCombine : AuditParamCombine?
    var detAddImages:((KSImageSelectUtil,[VideoImg])->())?
    var detAddFiles:((KSImageSelectUtil,[VideoImg])->())?
    var detDeleteImage:((KSImageSelectUtil,Int)->())?
    
    var one = [String:Any]() {
        didSet {
            self.configDataSource()
        }
    }
     func configDataSource() {
        
        for i in 0..<self.templateDetail.count {
            let value = any2Str(one[self.templateDetail[i].field_nm.unwrap])
            
            
            if self.templateDetail[i].field_type == TemplateType.radio.rawValue {
                (itemArr[i] as! KSSwitchItem).setSwitchOn(value == "1")
            } else if self.templateDetail[i].field_type == TemplateType.slidingblock.rawValue {
                (itemArr[i] as! KSBlockItem).setBlockValue(nm: value)
            } else if self.templateDetail[i].field_type == TemplateType.file.rawValue {
                
            } else {
                (itemArr[i] as! KSFormBaseItem).setVal(value)
            }
        }
    }
 //   *配置菜单高度流程*
    func configView(detO:DetailsTempCom) {
        
        self.templateDetail = detO.temp.filter { isShowJ($0,auCombine: self.auditParamCombine,exType: self.excuteType) }
        let t2Arr = self.templateDetail.compactMap{ $0.field_nm }
        let i2Arr = self.itemArr.compactMap{ $0.tagx }
 
         if t2Arr != i2Arr { // 首次初始化状态 或控件变化时
             KSLog("刷新了细表控件--")
             itemBg.removeAllItem()
             itemArr.removeAll()
             for i in 0..<self.templateDetail.count {
                 let item = self.templateDetail[i]
                 var preFer = itemPreferance(temp: item)
                 preFer.1.itembgColor = TempDetBg
                 preFer.1.textBgColor = TempDetBg

                var v:KSFormBaseItem!
                if item.field_type == TemplateType.radio.rawValue { // 开关型
                    v = KSSwitchItem(title: preFer.0,appearance:preFer.1 )
                    v.disabled = !isEnableJ(item,auCombine: self.auditParamCombine, exType: self.excuteType!)
                    v.tagx = item.field_nm.unwrap
                    itemBg.add(v)
                    itemArr.append(v)
                } else if item.field_type == TemplateType.slidingblock.rawValue { // 滑块型
                    v = KSBlockItem(title: preFer.0,appearance:preFer.1 )
                    v.disabled = !isEnableJ(item,auCombine: self.auditParamCombine, exType: self.excuteType!)
                    v.tagx = item.field_nm.unwrap
                     (v as! KSBlockItem).setBlockTitles(item.selectDataArr)
                    itemBg.add(v)
                    itemArr.append(v)
                } else if item.field_type  == TemplateType.file.rawValue { // 附件
                    var max = intTran(item.upload_num_max)
                    if max < 1 {
                        max = 1
                    }
                    let picTypeArr = type2PicType(item.upload_type.unwrap)
                    
                    var viewFlag:ImgViewType!
                    if isEnableJ(item,auCombine: self.auditParamCombine, exType: self.excuteType!) {
                        viewFlag = .editable
                    } else {
                        viewFlag = .viewOnly
                    }
                    let appe = KSImageSelectAppear()
                        .itembgColor(TempDetBg)
                    let imageU = KSImageSelectUtil(frame: CGRect(x:0,y:0,width:itemBg.ks_width ,height:singleImgHeight),httpHeader: USERINFO.picUrl.unwrap,maxImgCount: max,numberOfRow: 5,picType: picTypeArr,viewType:viewFlag,appear: appe)
                    imageU.backgroundColor = TempDetBg
                    imageU.collectionView?.backgroundColor = TempDetBg
                    imageU.ksDelegate = self
                    imageU.u_superVC = u_superVC
                    imageU.tagx = item.field_nm.unwrap
                    imageU.newestShow(arr: detO.imgArray)
                    imageU.ks_y = itemBg.tail!.frame.maxY
                    itemBg.addv(view: imageU)
                    itemArr.append(imageU)
                    // 关联001  单独处理圆角 内部淡蓝色圆角
                    if i == self.templateDetail.count - 1 {
                        imageU.setRoundType(type: .bottom,outColor: .white,innerColor: TempDetBg)
                      }
                } else {
                    if item.field_type == TemplateType.editlov.rawValue {
                        preFer.1.isEditAndActionBoth = true
//                        preFer.1.contentTrailing += 10
                    }
                    
                    if judgeUseVWidge(item) {
                        v = KSFormVItem(title: preFer.0,appearance:preFer.1,type: preFer.2)
                   
                    } else {
                        v = KSFormHItem(title: preFer.0,appearance:preFer.1,type: preFer.2)
                    }
                     
                    
                    if item.field_colour.unwrap != "" {
                        v.setTempTitle(preFer.0,color: UIColor(kshex:item.field_colour.unwrap ))
                    }
                    v.disabled = !isEnableJ(item,auCombine: self.auditParamCombine, exType: self.excuteType!)
                    v.tagx = item.field_nm.unwrap
                    itemBg.add(v)
                    itemArr.append(v)
                }
                 // 关联001  大部分统一处理圆角 内部淡蓝色圆角
                 if i == self.templateDetail.count - 1 {
                      if item.field_type  != TemplateType.file.rawValue {
                          v.setRoundType(type: .bottom,outColor: .white,innerColor: TempDetBg)
                     }
                  }
               
            }
        } else { // 个性化调整后的状态
    

            for i in 0..<self.templateDetail.count {
                let item = self.templateDetail[i]
                
              
                
                if item.field_type == TemplateType.file.rawValue {
                    (itemArr[i] as! KSImageSelectUtil).newestShow(arr: detO.imgArray)
                } else {
                    
                    var itemName = item.field_annotation.unwrap
                    if item.is_required == "1" {
                        itemName += "*"
                    }
                    (itemArr[i] as! KSFormBaseItem).setTempTitle(itemName)
 
                    (itemArr[i] as! KSFormBaseItem).disabled = !isEnableJ(item,auCombine: self.auditParamCombine, exType: self.excuteType!)
                    
                    (itemArr[i] as! KSFormBaseItem).appearance.contentHight(item.oneHeight)
                }
                
                
             }
            if itemBg.head.isSome{
                itemBg.reFrame(item: itemBg.head!)
            }
           
        }
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
         self.backgroundColor = .clear
         self.contentView.backgroundColor = Theme.awModuleBgColor
        contentView.addSubview(bottomBg)
        bottomBg.addSubview(itemBg)
        bottomBg.snp.makeConstraints { make in
            make.left.equalTo(self.contentView).offset(6)
            make.top.equalTo(self.contentView)
            make.right.equalTo(self.contentView).offset(-6)
            make.bottom.equalTo(self.contentView)
        }
        
        itemBg.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(10)
            make.top.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
        }
        
     
        self.itemBg.ks_width = KSGlobal.screenW - 32
     }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    
  
    
}
// MARK: 图片
extension ConfigMenuFormCell:KSImageUtilDelegate {
    
    func addImages(_ util:KSImageSelectUtil, imgvideos:[VideoImg]) {
        if let clo = self.detAddImages {
            var tts = imgvideos
            // 把名称  data 处理好
            for i in 0..<tts.count {
                let fdata = resetImgSize(sourceImage: tts[i].image!, maxImageLenght: filemaxImageLenght, maxSizeKB: filemaxSizeKB)
                tts[i].data = fdata
                let fname = getAttachName(i: i)
                
                if tts[i].type == .image {
                    tts[i].name = "\(fname).jpg"
                } else if tts[i].type == .video {
                    tts[i].name = "\(fname).mp4"
                } else {
                    tts[i].name = "\(fname).pdf"
                }
               
            }
            
            clo(util,tts)
        }
     }
    func  addFiles(_ util: KSImageSelectUtil, files: [VideoImg]) {
        if let clo = self.detAddFiles {
            clo(util,files)
        }
    }
    func deleteImage(_ util: KSImageSelectUtil, index: Int) {
        
        if let clo = self.detDeleteImage {
            clo(util,index)
        }
 
    }
}
